/*
 *--------------------------------------------------------------------------
 *   File Name:	call.so.speed.cc
 * 
 * Description:	测试对普通函数和动态链接库函数的调用的时间消耗对比。
 *
 * 		测试结果：动态链接库的耗时稍微要长一点，但不太明显。
 * 
 * 
 *      Author:	Zhao Yanbai [zhaoyanbai@126.com]
 * 		Information Security Research Center Of
 * 		Harbin Engineering University
 * 
 *     Version:	1.0
 * Create Date: Wed Jul 29 13:54:11 2009
 * Last Update: Wed Jul 29 13:54:11 2009
 * 
 *--------------------------------------------------------------------------
 */
#include<sys/time.h>
#include<stdlib.h>
#include<stdio.h>
#include<dlfcn.h>
#include<iostream>
using namespace std;
#define	COUNT	20000000000
void (*pf_testdlFun)(void *arg);
void	testFun1(void *arg)
{
}

void *ph;
char *perr;
int main()
{
	struct timeval tvBegin, tvEnd;
	unsigned long long i;
	unsigned long long usec;

	ph = dlopen("./so.so", RTLD_LAZY);
	if(ph == NULL)
	{
		cout<<"failed load so file"<<endl;
		exit(1);
	}
	perr = dlerror();
	if(perr != NULL)
	{
		cout<<"open libary error:"<<perr<<endl;
		exit(1);
	}

	*(void **) (&pf_testdlFun) = dlsym(ph,"testFundl");
	if(perr != NULL)
	{
		cout<<"function in libary error:"<<perr<<endl;
		exit(1);
	}
	//pf_testdlFun = testFun1;
	gettimeofday(&tvBegin, NULL);
	for(i=0; i<COUNT; i++) (*pf_testdlFun)(0);
	gettimeofday(&tvEnd, NULL);
	usec = (tvEnd.tv_sec - tvBegin.tv_sec) * 1000000
		+ tvEnd.tv_usec - tvBegin.tv_usec;
	cout<<"USEC:"<<usec<<endl;



	gettimeofday(&tvBegin, NULL);
	for(i=0; i<COUNT; i++) testFun1(0);
	gettimeofday(&tvEnd, NULL);
	usec = (tvEnd.tv_sec - tvBegin.tv_sec) * 1000000
		+ tvEnd.tv_usec - tvBegin.tv_usec;
	cout<<"USEC:"<<usec<<endl;

	return 0;
}
